<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\GrUserComment;
use App\Http\Controllers\DetailReply;
use App\Http\Controllers\Reply;
use App\Models\GrGood;
use App\Models\GrReply;
use app\Request;
use App\service\ReviewImages;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Widgets\Card;
use Dcat\Admin\Widgets\Modal;

class GrUserCommentController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new GrUserComment(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('goods_id');
            $grid->column('goods_grade')->using([
                1=>'2分',
                2=>'4分',
                3=>'6分',
                4=>'8分'
            ]);

            $grid->column('comment_content');
            $grid->column('serve_grade')->using([
                1=>'2分',
                2=>'4分',
                3=>'6分',
                4=>'8分'
            ]);
            $grid->column('comment_img')->display(function ($pictures) {

                return json_decode($pictures, true);

            })->image('', 100, 100);
            $grid->column('user_img')->image('',100,100);
            $grid->column('user_name');
            //搜索
            $grid->filter(function (Grid\Filter $filter) {
                $filter->panel();
                $filter->in('replay_status',"恢复状态")->multipleSelect(['0' => '未回复','1'=>'已恢复']);
                $filter->equal('user_name');
            });
            $grid->column('content')->display('回复')->modal(function (Grid\Displayers\Modal $modal) {
                // 标题
                $modal->title('弹窗标题');
                // 自定义图标
                $modal->icon('feather icon-edit');
                // 传递当前行字段值
                return Reply ::make()->payload(['id' => $this->id,'user_id'=>$this->user_id,'goods_id'=>$this->goods_id]);
            });
            //模态框
            $grid->detail->display('View')->expand(DetailReply::make(['post_type' => 1]));
            $grid->detail
                ->display('查看恢复内容') // 设置按钮名称
                ->modal(function ($modal) {
                    // 设置弹窗标题
                   // $card = new Card(null, $this->comment_content);
                    return DetailReply::make(['title' => $this->detail]);
                });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new GrUserComment(), function (Show $show) {
            $show->field('id');
            $show->field('goods_id');
            $show->field('goods_grade');
            $show->field('comment_content');
            $show->field('serve_grade');
            $show->field('comment_img');
            $show->field('comment_time');
            $show->field('user_img');
            $show->field('user_name');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new GrUserComment(), function (Form $form) {
            $form->display('id');
            $form->select('goods_id')->options('goods_id');
            //商品评分
            $form->radio('goods_grade')->options(['1' => '2', '2' => '4', '3' => '6', '4' => '8'])->required();
            //评论内容
            $form->text('comment_content');
            //服务评分
            $form->radio('serve_grade')->options(['1' => '2', '2' => '4', '3' => '6', '4' => '8'])->required();
            $form->multipleImage('comment_img')->saving(function ($paths) {
                return json_encode($paths);
            })->uniqueName()->autoUpload()->saveFullUrl();
            $form->image('user_img')->uniqueName()->autoUpload()->saveFullUrl();
            $form->text('user_name');
            $form->display('created_at');
            $form->display('updated_at');

            $form->saving(function (Form $form) {
                // 判断是否是新增操作
                if ($form->isCreating()) {
                    //判断用户头像是否合法
                    $img = $form->user_img;
                    if (!empty($img)) {
                        $reviewImage = new ReviewImages();
                        $res = $reviewImage->verify($img);
                        if ($res != "合规") {
                            return $form->response()->error('图片不合规');
                        }
                    }
                    $form->comment_time=date('Y-m-d H:i:s');
                    $form->user_id=1;
                    //判断评论图片是否合法
                    $goodsImg=$form->comment_img;
                    $goodsImage=explode(',',$goodsImg);
                    foreach ($goodsImage as $k=>$v){
                        $reviewImage = new ReviewImages();
                        $res = $reviewImage->verify($v);
                        if ($res != "合规") {
                            return $form->response()->error('图片不合规');
                        }
                    }
                    //判断评论内容是否合法
                    $commentContent=$form->comment_content;
                    $reviewImage = new ReviewImages();
                    $res = $reviewImage->text($commentContent);
                    if ($res != "合规") {
                        return $form->response()->error('文本不合法');
                    }

                }

            });


        });
    }

    /**
     * 查询商品
     */
    public function goods_id()
    {
        $data = GrGood::all()->toArray();
        foreach ($data as $k => $v) {
            $goodsIds[$k]['id'] = $v['goods_id'];
            $goodsIds[$k]['text'] = $v['goods_name'];

        }
        return $goodsIds;
    }
    //递归
    public static function getTree($data, $pid = 0)
    {
        $tress = [];
        foreach ($data as $v) {
            if ($v['pid'] == $pid) {
                $v['children'] = self::getTree($data, $v['reply_id']);
                $tress[] = $v;
            }
        }
        return $tress;
    }
        public function getComment(){
          //$data= \App\Models\GrUserComment::join('gr_reply','gr_user_comment.id','=','gr_reply.comment_id')->get()->toArray();
           $data=GrReply::where('comment_id',3)->get()->toArray();
            $d=self::getTree($data);
            return $d;
        }

}
